package com.cuit.zuke.admin.service.impl;

import com.cuit.zuke.admin.mapper.AdminMapper;
import com.cuit.zuke.admin.pojo.Admin;
import com.cuit.zuke.admin.pojo.Department;
import com.cuit.zuke.admin.pojo.ShowAdmin;
import com.cuit.zuke.admin.pojo.ShowDepartment;
import com.cuit.zuke.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    public Map<String, Object> queryShowAll(int currentPage, int pageSize){
/*        Map<String, Object> map = new HashMap<>();
        List<Admin> admins = this.queryAllAdmin();
        List<List<Long>> roles = new ArrayList<>();
        List<String> departs = new ArrayList<>();
        for (Admin admin : admins) {
            List<Long> role = this.queryAllRole(admin.getUserId());
            String depart = this.queryDepart(admin.getDepartmentId());
            roles.add(role);
            departs.add(depart);
        }
        map.put("admins", admins);
        map.put("roles", roles);
        map.put("departs", departs);
        return map;*/
        ShowAdmin show;
        List<ShowAdmin> shows = new ArrayList<>();
        List<Admin> admins = this.queryAllAdmin(currentPage, pageSize);
        for (Admin admin : admins) {
            show = new ShowAdmin();
            List<Long> role = this.queryAllRole(admin.getUserId());
            String depart = this.queryDepartName(admin.getDepartmentId());
            show.setAdmin(admin);
            show.setRole(role);
            show.setDepart(depart);
//            System.out.println(show);
            shows.add(show);
        }
        int total = this.adminTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("admin", shows);
        map.put("total", total);
        return map;
    }

    public List<Admin> queryAllAdmin(int currentPage, int pageSize) {
        return adminMapper.queryAllAdmin(currentPage, pageSize);
    }

    public int adminTotal() {
        return adminMapper.adminTotal();
    }

    public List<Long> queryAllRole(Long userId) {
        return adminMapper.queryAllRole(userId);
    }

    public String queryDepartName(Long departmentId) {
        return adminMapper.queryDepartName(departmentId);
    }

    public List<ShowAdmin> queryShowOne(Long userId) {
        ShowAdmin show = new ShowAdmin();
        List<ShowAdmin> showOne = new ArrayList<>();
        Admin admin = this.queryUserById(userId);
        if(admin != null) {
            List<Long> role = this.queryAllRole(userId);
            String depart = this.queryDepartName(admin.getDepartmentId());
            show.setAdmin(admin);
            show.setRole(role);
            show.setDepart(depart);
//            System.out.println(show);
            showOne.add(show);
            return showOne;
        }
        return null;
    }

    public Admin queryUserById(Long userId) {
        return adminMapper.queryUserById(userId);
    }

    public List<Department> queryAllDepart() {
        return adminMapper.queryAllDepart();
    }

    public Department queryFatherDepartByParentID(Long parentId) {
        return adminMapper.queryFatherDepartByParentID(parentId);
    }

    public List<Department> queryChildDepartByParentID(Long id) {
        return adminMapper.queryChildDepartByParentID(id);
    }

    public List<ShowDepartment> ShowDepartment() {
        //获取所有部门
        List<Department> departments = this.queryAllDepart();
        //顶层部门
        ShowDepartment topFather = new ShowDepartment();
        //查出顶层父公司 topFather
        for (Department department : departments) {
            Long parent_id = department.getParentId();
            Department parent = this.queryFatherDepartByParentID(parent_id);
            //说明没有父公司
            if(parent == null) {
                topFather.setId(department.getId());
                topFather.setName(department.getName());
                topFather.setPhone(department.getPhone());
                topFather.setAddress(department.getAddress());
                topFather.setCreate_time(department.getCreateTime());
                topFather.setModified_time(department.getModifiedTime());
                topFather.setIs_deleted(department.getIsDeleted());
                topFather.setParent_id(department.getParentId());
                //暂时赋值为null
                topFather.setChildren(null);
            }
        }
        //顶层父公司的孩子
        List<ShowDepartment> topChildren = new ArrayList<>();
        //获得顶层部门的id
        Long topId = topFather.getId();
        //查出第二层部门
        List<Department> second = this.queryChildDepartByParentID(topId);
        for (Department department : second) {
            //暂存第二层部门
            ShowDepartment secondFather = new ShowDepartment();
            //第二层父公司的id
            Long secondId = department.getId();
            //查出第三层部门
            List<Department> third = this.queryChildDepartByParentID(secondId);
            //装第二层部门的孩子
            List<ShowDepartment> children = new ArrayList<>();
            //将第三层部门装进第二层的Children
            for (Department department1 : third) {
                ShowDepartment child = new ShowDepartment();
                child.setId(department1.getId());
                child.setName(department1.getName());
                child.setPhone(department1.getPhone());
                child.setAddress(department1.getAddress());
                child.setCreate_time(department1.getCreateTime());
                child.setModified_time(department1.getModifiedTime());
                child.setIs_deleted(department1.getIsDeleted());
                child.setParent_id(department1.getParentId());
                child.setChildren(null);
                children.add(child);
            }
            //给第二层部门赋值
            secondFather.setId(department.getId());
            secondFather.setName(department.getName());
            secondFather.setPhone(department.getPhone());
            secondFather.setAddress(department.getAddress());
            secondFather.setCreate_time(department.getCreateTime());
            secondFather.setModified_time(department.getModifiedTime());
            secondFather.setIs_deleted(department.getIsDeleted());
            secondFather.setParent_id(department.getParentId());
            secondFather.setChildren(children);
            topChildren.add(secondFather);
        }
        topFather.setChildren(topChildren);
        //返回的部门数据
        List<ShowDepartment> list = new ArrayList<>();
        list.add(topFather);

        return list;
    }

    public List<Admin> queryAdminByChildDepartmentId(Long departmentId, int currentPage, int pageSize) {
        return adminMapper.queryAdminByChildDepartmentId(departmentId, currentPage, pageSize);
    }

    public List<Admin> queryAdminByFatherDepartmentId(Long departmentId, int currentPage, int pageSize) {
        return adminMapper.queryAdminByFatherDepartmentId(departmentId, currentPage, pageSize);
    }

    public int queryAdminTotalByChildDepartmentId(Long departmentId) {
        return adminMapper.queryAdminTotalByChildDepartmentId(departmentId);
    }

    public int queryAdminTotalByFatherDepartmentId(Long departmentId) {
        return adminMapper.queryAdminTotalByFatherDepartmentId(departmentId);
    }

    public Map<String, Object> getShowByDepartmentId(Long departmentId, int currentPage, int pageSize) {
        if(departmentId == 1) {
            return this.queryShowAll(currentPage, pageSize);
        }
        //该部门下的人数
        int total = 0;
        //返回给前端的页面
        List<ShowAdmin> show = new ArrayList<>();
        //根据departmentId获取该id的孩子
        List<Department> child = this.queryChildDepartByParentID(departmentId);
        //为孩子部门
//        if(child.size() == 0) {
//            total = this.queryAdminTotalByDepartmentId(departmentId);
//            List<Admin> admins = this.queryAdminByDepartmentId(departmentId, currentPage, pageSize, flag);
//            for (Admin admin : admins) {
//                Long userId = admin.getUserId();
//                List<ShowAdmin> one = this.queryShowOne(userId);
//                show.add(one.get(0));
//            }
//        }
//        //为第二层父部门
//        else {
//            flag = 1;
//            for (Department department : child) {
//                Long id = department.getId();
//                total = total + this.queryAdminTotalByDepartmentId(id);
//                List<Admin> admins = this.queryAdminByDepartmentId(id, currentPage, pageSize, flag);
//                for (Admin admin : admins) {
//                    Long userId = admin.getUserId();
//                    List<ShowAdmin> one = this.queryShowOne(userId);
//                    show.add(one.get(0));
//                }
//            }
//        }

        List<Admin> admins;
        if(child.size() == 0) {
            admins = this.queryAdminByChildDepartmentId(departmentId, currentPage, pageSize);
            total = this.queryAdminTotalByChildDepartmentId(departmentId);
        }else {
            admins = this.queryAdminByFatherDepartmentId(departmentId, currentPage, pageSize);
            total = this.queryAdminTotalByFatherDepartmentId(departmentId);
        }
        System.out.println(admins);
        for (Admin admin : admins) {
            Long userId = admin.getUserId();
            List<ShowAdmin> one = this.queryShowOne(userId);
            show.add(one.get(0));
        }

        Map<String, Object> map = new HashMap<>();
        map.put("admin", show);
        map.put("total", total);
        return map;
    }

    /*根据userid删除管理员*/
    public void deleteAdmin(Long userId) {
        adminMapper.deleteAdmin(userId);
    }
}
